#!/usr/bin/env python

# --------------------------------------
# projects/python/collatz/TestCollatz.py
# Copyright (C) 2009
# Glenn P. Downing
# --------------------------------------

# To run the tests
#     TestCollatz.py

# To document the tests
#     pydoc -w TestCollatz

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestCollatz
# -----------

class TestCollatz (unittest.TestCase) :
    # ----
    # read
    # ----

    def test_read (self) :
        reader = TestReader('1 10\n')
        main.my_read(reader)
        self.assert_(main.i ==  1)
        self.assert_(main.j == 10)

    # ----
    # eval
    # ----

    def test_eval1 (self) :
        main.i =  1
        main.j = 10
        main.v =  0
        main.my_eval()
        self.assert_(main.v == 20)

    def test_eval2 (self) :
        main.i = 100
        main.j = 200
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 125)

    def test_eval3 (self) :
        main.i = 201
        main.j = 210
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 89)

    def test_eval4 (self) :
        main.i =  900
        main.j = 1000
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 174)

    def test_eval5 (self) :
        main.i = 5
        main.j = 5
        main.v = 0
        main.my_eval()
        self.assert_(main.v == 6)

    def test_eval6 (self) :
        main.i = 5
        main.j = 6
        main.v = 0
        main.my_eval()
        self.assert_(main.v == 9)

    def test_eval7 (self) :
        main.i = 6
        main.j = 5
        main.v = 0
        main.my_eval()
        self.assert_(main.v == 9)

    def test_eval8 (self) :
        main.i = 5000
        main.j = 6000
        main.v = 0
        main.my_eval()
        self.assert_(main.v == 236)

    def test_eval9 (self) :
        main.i = 8888
        main.j = 9999
        main.v = 0
        main.my_eval()
        self.assert_(main.v == 260)
    
    def test_eval10 (self) :
        """
        this is to test the assertions and invalid input handling.
        """
        main.i = "foo"
        main.j = "bar"
        main.v = 0
        try :
            main.my_eval()
            self.assert_(false) #should never reach here
        except AssertionError, e :
            pass
    
    def test_eval11 (self) :
        """
        tests negative numbers for i,j
        """
        main.i = -1
        main.j = -3
        main.v = 0
        
        try :
            main.my_eval()
            self.assert_(false) #should never reach here
        except AssertionError, e :
            pass
            
    # -----
    # print
    # -----

    def test_print (self) :
        main.i =  1
        main.j = 10
        main.v = 20
        writer = TestWriter()
        main.my_print(writer)
        self.assert_(writer.str() == '1 10 20\n')

if __name__ == "__main__" :
    unittest.main()


